package kq

import (
	"context"
	"encoding/json"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"navi-mumbai/app/mq/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/kqmsg"
)

/*
*
Listening to the payment flow status change notification message queue
*/
type AddUserNotifyMsgActionMq struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddUserNotifyMsgActionMq(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserNotifyMsgActionMq {
	return &AddUserNotifyMsgActionMq{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddUserNotifyMsgActionMq) Consume(_, val string) error {
	var message kqmsg.KqAddUserNotifyMsg
	err := json.Unmarshal([]byte(val), &message)
	if err != nil {
		l.Logger.Errorf("AddUserNotifyMsgActionMq->Consume Unmarshal err : %+v , val: %s", err, val)
		return err
	}

	err = l.execService(&message)
	if err != nil {
		l.Logger.Errorf("AddUserNotifyMsgActionMq->execService err : %+v , val :%s , message:%+v", err, val, message)
		return err
	}
	return nil
}

func (l *AddUserNotifyMsgActionMq) execService(msg *kqmsg.KqAddUserNotifyMsg) (err error) {
	var in pb.AddUserNotifyMsgReq
	_ = copier.Copy(&in, msg)
	_, err = l.svcCtx.UcSysRpc.AddUserNotifyMsg(l.ctx, &in)
	if err != nil {
		l.Logger.Errorf("AddUserNotifyMsgActionMq->execService err : %+v , msg : %+v", err, msg)
		return err
	}
	return nil
}
